#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :抖音头条指数.py
# @Time      :2024/11/26 
# @Author    :CL
# @email     :1037654919@qq.com

from scrapy_juliangsuanshu import *




# 抖音头条指数
def douyin_toutiao_index_run(keywords, start_date, end_date):
    print('begin function:douyin_toutiao_index')
    app_names = ['aweme', 'toutiao']
    runtimes = 0
    #app_names = [ 'toutiao']   #临时
    for app_name in app_names:
        for keyword in tqdm(keywords):
            print('%s正在查询：%s' % (app_name, keyword))
            datax = pd.DataFrame()
            try:
                datastr = douyin_toutiao_index(keyword=keyword, start_date=start_date, end_date=end_date, app_name=app_name)
                datas = decrtptlx(datastr)
                datas = eval(datas)
                data = datas['hot_list']
                for value in data:
                    data2 = value['hot_list']
                    data3 = value['search_hot_list']
                    for (value2, value3) in zip(data2, data3):
                        datax = datax.append(
                            [{'keyword': keyword, 'date': value2['datetime'], 'index': value2['index'],
                              'search_index': value3['index'], 'app_name': app_name}],
                            ignore_index=True)
                    yield datax
            except TypeError as ee:
                print("something wrong with the code,%s %s fialed:%s" % (keyword, app_name, ee))


            runtimes += 1
            if (runtimes % 20 == 0):
                print('take a rest :20s')
                time.sleep(20)
            time.sleep(5)  # 防止被封，多停顿一些时间

if __name__ == '__main__':
    douyin_toutiao_index_run()
